﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.UI.WebControls;
using DotNetNuke.Entities.Modules.Communications;

using CharityGamesOnline.Modules.Common;

namespace CharityGamesOnline.Modules.Event
{
    public partial class EventListSimple : CGOBase
    {
        private int charityID = 0;

        public int CharityID
        {
            get
            {
                if (charityID == 0)
                {
                    if ((Request.QueryString["CharityID"] != null))
                    {
                        charityID = Int32.Parse(Request.QueryString["CharityID"]);
                    }
                }

                return charityID;
            }
        }

        public int EventDetailTabID
        {
            get
            {
                if (!Null.IsNull(Settings["EventDetailTabID"]))
                {
                    return Int32.Parse(Settings["EventDetailTabID"].ToString());
                }
                else
                    return TabId;
            }
        }

        public int GameDetailTabID
        {
            get
            {
                if (!Null.IsNull(Settings["GameDetailTabID"]))
                {
                    return Int32.Parse(Settings["GameDetailTabID"].ToString());
                }
                else
                    return 0;
            }
        }

        public bool boolViewArchive
        {
            get
            {
                if (!Null.IsNull(Settings["ShowArchive"]))
                {
                    return Boolean.Parse(Settings["ShowArchive"].ToString());
                }
                else
                    return false;
            }

        }

        #region Event Handler

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    BindData();
                }
            }

            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void dlEvent_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HyperLink lnkEventDetail = e.Item.FindControl("lnkEventDetail") as HyperLink;

                int eventID = Int32.Parse(DataBinder.Eval(e.Item.DataItem, "EventID").ToString());

                if (EventDetailTabID == TabId || EventDetailTabID < 0)
                {
                    lnkEventDetail.NavigateUrl = Globals.NavigateURL(EventDetailTabID, string.Empty, "mid=" + ModuleId.ToString(), "mctl=EventDetail", "EventID=" + eventID.ToString());
                }
                else
                {
                    lnkEventDetail.NavigateUrl = Globals.NavigateURL(EventDetailTabID, string.Empty, "EventID=" + eventID.ToString());
                }
            }
        }

        #endregion

        #region Private Function

        private void BindData()
        {
            EventController ctrlEvent = new EventController();
            List<EventInfo> lstEvent;

            if (boolViewArchive)
            {
                lstEvent = ctrlEvent.GetEvents(PortalId, CharityID, Null.NullDate, DateTime.Now.Date);
            }
            else
            {
                lstEvent = ctrlEvent.GetEvents(PortalId, CharityID, DateTime.Now.Date);
            }
            if (lstEvent.Count > 0)
            {
                lbMessage.Visible = false;
                dlEvent.Visible = true;
                dlEvent.DataSource = lstEvent;
                dlEvent.DataBind();
            }
            else
            {
                lbMessage.Visible = true;
                dlEvent.Visible = false;
                lbMessage.Text = Localization.GetString("lbNoActiveEvent", LocalResourceFile);                
            }
        }

        #endregion

        #region Public Function

        public void BindData(int intCharity)
        {
            charityID = intCharity;
            BindData();
        }

        #endregion
    }
}